#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdlib.h>


extern char** environ;
//进程替换
int main()
{
  pid_t id = fork();
  if(id==0)
  {
    //child,替换的两个条件，在哪？怎么执行?
    char* const argv[10] = {(char*)"ls",(char*)"-a",(char*)"-l",NULL};
    char* const argv_[2] = {(char*)"top",NULL};
    char* const argv1[2] = {(char*)"cmd",NULL};
    char* const env[10] =  {
    (char*) "MYPATH=suzm",
      NULL };
    int ret = execve("./cmd",argv1,environ);

    //int ret = execvp(argv[0],argv);
    //int ret = execlp(argv[0],"ls","-a","-l",NULL);
    //int ret  = execv("/usr/bin/ls",argv);
    //int ret =  execl("/usr/bin/ls","ls","-a","-l",NULL);//一切指针数组已NULL结尾
   printf("进程替换失败码%d\n",ret); 
   exit(1);            //到了这一步，就说明进程替换失败了，直接退出
  }
  int status = 0;
  pid_t ret = waitpid(id,&status,0);//option填WNOHANG表示非阻塞等待,0表示阻塞等待
  if(ret>0)
  {
    printf("进程等待成功，我是父进程%d,子进程pid是%d,退出码%d,退出信号%d\n",getpid(),id,(status>>8)&0xFF,status&0x7F);
  }
  else 
  {
    printf("等待失败\n");
  }
  return 0;
}
